package com.example.spring0909.section3.proxy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;


//基于注解的切面类
//基于注解的切面类
//基于注解的切面类
//基于注解的切面类
//基于注解的切面类

@Component  //实现bean的注入 实例化到Spring容器中 相当于SSM配置文件中的<bean class=>
@Aspect
public class MyAnnotationAspect {

    //配置切入点

    @Pointcut("execution(* com.example.spring0909.section3.service.impl.*.*(..))")
    public void pc1(){}

    @Pointcut("execution(int com.example.spring0909.section3.service.impl.OrderServiceImpl.update(..))")
    public void pc2(){}

    @Before("pc1()")
    public void before(){
        System.out.println("切点方法before执行了...(MyAnnotationAspect)");
    }

    @After("pc1()")
    public void after() {
        System.out.println("切点方法after执行了...(MyAnnotationAspect)");
    }

    @AfterReturning(value = "pc2()",returning = "a")
    public void afterReturn(int a){
        System.out.println("切点方法afterReturn执行了...(MyAnnotationAspect)");
        System.out.println("使用的返回值是"+a);
    }

    @AfterThrowing("pc1()")
    public void afterThrowing(){
        System.out.println("切点方法afterThrowing执行了...(MyAnnotationAspect)");
    }

    @Around("pc1()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        System.out.println("环绕通知: 切入点方法执行之前的增强业务逻辑在此执行了...(MyAnnotationAspect)");
        long begin = System.currentTimeMillis();

        Object proceed = point.proceed();

        System.out.println("环绕通知: 切入点方法执行之后的增强业务逻辑在此执行了...(MyAnnotationAspect)");
        long end = System.currentTimeMillis();
        System.out.println("当前方法调用总耗时:" + (end-begin)+"毫秒");

        return proceed;
    }
}
